<?php 
  $department = [
    ['id' => 1,'name' => 'John','age' => 14, 'title' => 'Mannager','salary' => 18000],
    ['id' => 2,'name' => 'Aaron','age' => 23, 'title' => 'Boss','salary' => 21000],
    ['id' => 3,'name' => 'Caesar','age' => 54, 'title' => 'DiedMan','salary' => 8888],
    ['id' => 4,'name' => 'Bai-Li','age' => 110, 'title' => 'Poeter','salary' => 6666],
    ['id' => 5,'name' => 'Suprise','age' => 119, 'title' => 'fucker','salary' => 7777],
    ['id' => 6,'name' => 'Test1','age' => 14, 'title' => 'Something','salary' => 2323]
  ];
 /*
  if(isset($_GET['s'])&& $_GET['s'] == 'age')
  {
    usort($department, 'compareByAge');
  }
  if (isset($_GET['m'])&& $_GET['m'] == 'salary')
  {
  	usort($department, 'compareBySalary');
  }
  */
  /**
  *实现按年龄排序 由低到高
  *@param
  *@return  返回大于0的数，表示$emp1>$emp2 反之， 等于0表示相同。
  */
  function compareByAge ($emp1,$emp2)
  {
  	return ($emp1['age']-$emp2['age']);
  }
  function comparByName ($emp1,$emp2)
  {
    return strcmp($emp1['name'], $emp2['name']);
  }
  function compareById($emp1,$emp2)
  {
  	return ($emp1['id']-$emp2['id']);
  }
  /**
  *实现按薪资排序 由高到低
  *
  *@return 返回大于零的数 表示$emp2>$emp1 反之， 等于零 表示两数相同。
  */
  function compareBySalary ($emp1,$emp2)
  {
  	return ($emp2['salary'] - $emp1['salary']);
  }
  //默认请求方式GET  执行下面
  $data = $department;
  //判断请求条件是不是post 如果是 执行下面如果不是执行上面
  if ($_SERVER['REQUEST_METHOD'] === 'POST') 
  {
  	//trim忽略空格 ，htmlspecialchars 过滤特殊字符
  	$searchKey = isset($_POST['search_key']) ?trim(htmlspecialchars($_POST['search_key'])):'';
  	//$searchKey = htmlspecialchars($searchKey);//过滤 特殊字符
  	$searchDate = [];//空数组  用来存放符合条件的项
  	//遍历 查找和title 相同的数据 并且存放到data数组中
  	foreach ($department as $emp) 
  	{
  		//对员工的title进行匹配判断
  		if (stripos($emp['title'],$searchKey) === 0)
  		  {
  			$searchDate[] = $emp;
  	      }
  	      elseif ($searchKey === '') {
  	      	$searchDate[] = $emp;
  	      } 
    }
     $data = $searchDate;  
  }
  	$sortKey =isset($_POST['sort_key']) ? $_POST['sort_key'] :0;
  	switch ($sortKey) {
  		case '0':
  			usort($data, 'compareById');
  			break;
      case '1':
        usort($data, 'comparByName');
        break;
  		case '2':
  			usort($data, 'compareByAge');
  			break;
  		case '3':
  			usort($data, 'compareBySalary');
  			break;
  	}
 ?>
 <!DOCTYPE html>
 <html>
 <head>
   <title>表格数据</title>
   <style>
     #wrapper {
     	width: 90%;
     	min-height: 290px;
     	margin: 0 auto;
     	padding-top: 10px;
     }
     #wrapper table, #wrapper table th, #wrapper table td {
     	border: 1px solid #ccc;
     	border-collapse: collapse;
     	border-spacing: 0;
     }
     #wrapper table {
     	width: 100%;
           margin: 0 auto;
    }
    #wrapper table tr {
    	height: 40px;
    }
    #wrapper table th {
    	font-size: 16px;
    	font-weight: bolder;
    	color: #222;
    	background-color: #f9f9f9; 
    }
    #wrapper table td {
    	text-align: center;
    }
    #wrapper table .data-row:hover {
    	background: #e9e9e9;
    }
    .btn {
	  display: block;
	  width: 100px;
	  height: 45px;
	  line-height: 45px;
	  text-align: center;
	  color: #777;
	  text-decoration: none;
	  background-color: #cecece;
	  border-radius: 6px;
	  margin: 0px;
    }
    .btn:hover{background-color:#dfdfdf;}
    .a_detail {
    	text-decoration: none;
    	color: #66ccff;
    }
    .a_detail:hover{color: #dfdfdf;}
   </style>
 </head>
 <body>
   <div>
   	<form method="post">
   		<input type="text" name="search_key" placeholder="请输入title模糊查找" />
   		<input type="submit" value="查找">
   	</form>
   </div>
   <div>
   		<form method="post">
   			<label for="sort_by">排序</label>
   			<select name="sort_key" id="sort_by">
   			<option value="0">id</option>
        <option value="1">name</option>
   			<option value="2">age</option>
   			<option value="3">salary</option>
   			</select>

   			<input type="submit" value="确定">
   		</form>
   </div>
   <div id="wrapper">
   	<table>
   		<tr>
   			<th>id</th>
   			<th>name</th>
   			<th>age</th>
   			<th>title</th>
   			<th>salary</th>
   			<th>detail</th>
   		</tr>
   		<?php foreach ($data as $emp): ?>
   		<tr class="data-row">
   			<td><?= $emp['id'] ?></td>
   			<td><?= $emp['name'] ?></td>
   			<td><?= $emp['age'] ?></td>
   			<td><?= $emp['title'] ?></td>
   			<td><?= $emp['salary'] ?></td>
   			<td><a class="a_detail" href="data_detail.php?id=<?php echo $emp['id'] ?>&n=<?php echo $emp['name'] ?>&a=<?php echo $emp['age'] ?>&t=<?php echo $emp['title'] ?>&s=<?php echo $emp['salary'] ?>">Detail</a></td>
   		</tr>
   		<?php endforeach; ?>
   	</table>
   </div>
 </body>
 </html>